package com.twd.mynio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class MyClient {
    public static void main(String[] args) throws IOException {
        SocketChannel client = SocketChannel.open();
        client.configureBlocking(false);
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
//        boolean connect = client.connect(new InetSocketAddress("127.0.0.1", 9988));
//        client.finishConnect();
//连接服务器
        if (!client.connect(new InetSocketAddress("127.0.0.1",9988))) {
            while(!client.finishConnect()){
                System.out.println("因为连接需要时间,客户端不会阻塞,可以做其他工作...");
            }
        }
//        assert connect;
        while(scanner.hasNext()){
            String next = scanner.next();
            ByteBuffer wrap = ByteBuffer.wrap("asa".getBytes());
//            byteBuffer.flip();
            client.write(wrap);
//            byteBuffer.clear();
        }
    }
}
